const app = getApp()
const WXAPI = require("../../../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lodingtype: 1, //加载状态（0：加载中  1：加载完成没有更多）
    cardLs: [],
    tabLs: [{
        id: 1,
        value: '使用中'
      }, {
        id: 4,
        value: '已用完'
      },
      // {
      //   id: 2,
      //   value: '转卖中'
      // }, 
      {
        id: 3,
        value: '已失效'
      },
      {
        id: 5,
        value: '退卡'
      }
    ],
    cardStatus: 1, //卡状态 1 使用中 2 转卖 3 过期 4 已用完 5 退卡
    pageSize: 10,
    pageNum: 1,
    cardTotal: 0,
    lodingtype: 1, //加载状态（0：加载中  1：加载完成没有更多）
    imgHttp: app.globalData.imgHttp, //图片前缀
    giveType: '', //赠送类型
    assetsStatus: '', //状态
    changeIndex: 1,
    isPull: true,
    cardId: '', //卡id
    type: '', //类型 1充值 2充值奖励 3分红 4邀请奖励 5消费
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight: app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom
    })
    // this.getdict()
    // this.getdictStatus()
    // this.data.cardId = options.id
    let renovate = wx.getStorageSync('renovate')
    if (renovate != '1') {
      console.log('返回不刷新')
      this.getCardls()
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    // this.setData({
    //   pageNum: 1
    // })
    // this.getLs()
    let renovate = wx.getStorageSync('renovate')
    if (renovate == '1') {
      console.log('返回刷新')
      this.setData({
        pageNum: 1,
        changeIndex: 1,
        cardStatus: 1,
      })
      this.getCardls()
    }

  },
  //下拉刷新
  onPullDownRefresh() {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("触底了", this.data.pageNum, Math.ceil(this.data.cardTotal / this.data.pageSize), this.data.cardTotal)
    if (this.data.pageNum >= Math.ceil(this.data.cardTotal / this.data.pageSize)) {
      console.log('没有分页')
      this.setData({
        lodingtype: 1
      })
    } else {
      this.setData({
        lodingtype: 0
      })
      this.data.pageNum++; //每触底一次 page + 1;
      this.getCardls();
    }
  },
  changeTab(e) { //选择类型
    let id = e.currentTarget.dataset.id
    this.setData({
      changeIndex: id,
      cardStatus: id,
    })
    this.data.pageNum = 1
    this.getCardls()
    console.log(id)
  },
  back() {
    wx.navigateBack({
      delta: 0,
    })
  },
  //充值
  jumpRecharge(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/userPageA/pages/topup/topup?customerCardId=' + id,
    })
  },
  getCardls() { //获取股东卡列表
    wx.showLoading({
      title: '加载中',
    })
    var data = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      status: this.data.cardStatus
    }
    WXAPI.cardList(data, (res) => {
      const data = res.result
      if (res.code == 200) {
        // this.data.cardtotal = res.result.total
        if (res.result.total == -1) {
          this.data.cardTotal = 0
        } else {
          this.data.cardTotal = res.result.total
        }
        this.setData({
          cardTotal: this.data.cardTotal
        })
        console.log("判断", this.data.pageNum, Math.ceil(this.data.cardTotal / this.data.pageSize), this.data.cardTotal)
        if (this.data.pageNum >= Math.ceil(this.data.cardTotal / this.data.pageSize)) {
          this.setData({
            lodingtype: 1
          })
        } else {
          this.setData({
            lodingtype: 0
          })
        }

        if (this.data.pageNum > 1) {
          this.data.cardLs = this.data.cardLs.concat(res.result.records); //将数据拼接在一起
          this.setData({
            cardLs: this.data.cardLs
          })
        } else {
          this.data.cardLs = res.result.records
          this.setData({
            cardLs: this.data.cardLs
          })
        }
        wx.hideLoading({})
      }
    })
  },
  jumpCard(e) { //跳转我的股东卡
    let item = e.currentTarget.dataset.item
    let resellId = item.resellId
    var cardId = item.id
    if (item.lookStatus == 1) {
      var url = `/app/v2/customer/asset/card/dividend/look/${cardId}`
      WXAPI.dividendLook(url, '', (res) => {
        const {
          result
        } = res
        if (res.code === 200) {
          console.log('已读')
          this.data.pageNum = 1
          this.getCardls()
        }
      })
    }
    if (item.cardType == 8) {
      wx.navigateTo({
        url: '/userPageC/pages/abulkOrderDetail/abulkOrderDetail?customerCardId=' + item.id,
      })
    } else {
      // status：1 正常、2 转卖中、3 已过期、 4 已退卡待退款、5 已退款
      if (item.status != 2) {
        wx.navigateTo({
          url: '/ecCanvas/pages/myShareholderCard/otherCarddetail/otherCarddetail?assetId=' + item.assetId + '&id=' + item.id,
        })
      }
      // if (item.status == 1 || item.status == 4 || item.status == 5) {
      //   wx.navigateTo({
      //     url: '/ecCanvas/pages/myShareholderCard/otherCarddetail/otherCarddetail?assetId=' + item.assetId + '&id=' + item.id,
      //   })
      // }else if (item.status == 3 && item.cardType == 5) {
      //   wx.navigateTo({
      //     url: '/ecCanvas/pages/myShareholderCard/otherCarddetail/otherCarddetail?assetId=' + item.assetId + '&id=' + item.id,
      //   })
      // }
      else if (item.status == 2) {
        wx.navigateTo({
          url: '/userPageB/pages/userStore/cardResell/cardResell?resellId=' + resellId,
        })
      }
    }

  },
  jump(e) { //跳转详情
    console.log(e.currentTarget.dataset.index)
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    console.log(name)
    let status = 0
    if (name == '' && e.currentTarget.dataset.status != 2 && e.currentTarget.dataset.status != 1 && e.currentTarget.dataset.status != 4) {
      status = 6
    } else {
      status = e.currentTarget.dataset.status
    }
    if (e.currentTarget.dataset.index == 1) {
      wx.navigateTo({
        url: '/userPageA/pages/wallet/record_detailGive/record_detailGive?id=' + id + '&status=' + status,
      })
    } else {
      wx.navigateTo({
        url: '/userPageA/pages/wallet/record_detailReceive/record_detailReceive?id=' + id,
      })
    }
  },
})